//
//  CheckBalnkViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/11.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CheckBalnkViewController.h"
#import "FindWordViewController.h"
#import "ZJSecuritycodeViewController.h"
@interface CheckBalnkViewController ()<UITextFieldDelegate>

@property(nonatomic ,strong) UITextField *passWord;
@property(nonatomic ,strong) UITextField *passWord1;
@property(nonatomic ,strong) UILabel *bankName;
@property(nonatomic ,strong) UILabel *bankCount;
@property(nonatomic ,strong) UILabel *label;
@property(nonatomic ,strong) NSString *bankString;
@property(nonatomic ,strong) UIImageView *bankImage;

@end

@implementation CheckBalnkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)createView{
    self.title=@"验证信息";
    UIView *first=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 100)];
    first.backgroundColor=[UIColor whiteColor];
    //[self.view addSubview:first];
    MYLabel*blankName=[[MYLabel alloc]init];
    blankName.text=@"银行卡号:";
    blankName.textColor=TextColor6;
    [first addSubview:blankName];
    CGSize size=[blankName.text sizeWithAttributes:@{NSFontAttributeName:blankName.font}];
    blankName.frame=CGRectMake(10, 15, size.width+20, 20);
    MYLabel*BlankNum=[[MYLabel alloc]init];
    BlankNum.text=@"卡号号号:";
    BlankNum.textColor=TextColor6;
    [first addSubview:BlankNum];
    BlankNum.frame=CGRectMake(10, 65, size.width+10, 20);
    
    MYLabel*blank=[[MYLabel alloc]initWithFrame:CGRectMake(10+size.width+25, 0, MainW-20, 49)];
    blank.text=MyString(@"招商银行储蓄卡");
    blank.textColor=TextColor3;
    self.bankCount=blank;
    //self.passWord.keyboardType=UIKeyboardTypeNumberPad;
    [first addSubview:blank];
    
    MYLabel*number=[[MYLabel alloc]initWithFrame:CGRectMake(10+size.width+25, 50, MainW-20, 49)];
    number.text=@"3457689006787980";
    self.bankName=number;
    number.textColor=TextColor3;
    [first addSubview:number];
    
    
    
    
    UIView *bottom=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 100)];
    bottom.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bottom];
    MYLabel*UserName=[[MYLabel alloc]init];
    UserName.text=MyString(@"银行卡号:");
    UserName.textColor=TextColor6;
    [bottom addSubview:UserName];
    MYLabel*AcooutName=[[MYLabel alloc]init];
    AcooutName.text=MyString(@"持卡人");
    AcooutName.textColor=TextColor6;
    [bottom addSubview:AcooutName];
    
    
    self.passWord=[[UITextField alloc]initWithFrame:CGRectMake(10, 30, MainW-20, 49)];
    self.passWord.placeholder=MyString(@"卡号");
    [self.passWord setValue:[UIFont boldSystemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];
    self.passWord.font=FontAndStyle14;
    self.passWord.textColor=TextColor3;
    self.passWord.delegate=self;
    //self.passWord.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord.borderStyle=0;
    [bottom addSubview:self.passWord];
    UIView *v=[[UIView alloc]init];
    v.backgroundColor=Gray_Color;
    [bottom addSubview:v];
    self.passWord1=[[UITextField alloc]initWithFrame:CGRectMake(10, 110, MainW-20, 49)];
    self.passWord1.font=FontAndStyle14;
    self.passWord1.textColor=TextColor3;
    self.passWord1.placeholder=MyString(@"持卡人姓名");
    [self.passWord1 setValue:[UIFont boldSystemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];
    //self.passWord1.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord1.borderStyle=0;
    [bottom addSubview:self.passWord1];
    
    
    self.bankImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 107.5, 15, 15)];
    self.bankImage.hidden=YES;
    [self.view addSubview:self.bankImage];
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(30, 110, MainW-20, 10)];
    self.label.font=[UIFont systemFontOfSize:10];
    self.label.textColor=TextColor9;
    self.label.hidden=YES;
    [self.view addSubview:self.label];
    
    
    
    UIButton *MySureBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 220, MainW-20, 50)];
    [MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [MySureBtn setTitle:MyString(@"下一步") forState:UIControlStateNormal];
    [MySureBtn addTarget:self action:@selector(SureClick) forControlEvents:UIControlEventTouchUpInside];
    [MySureBtn setBackgroundColor:Red_Color];
    [self.view addSubview:MySureBtn];
    ViewRadius(MySureBtn, 5);
    
    
    [UserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo((50-size.height)/2);
        make.left.equalTo(10);
        make.width.mas_equalTo(size.width+5);
        make.height.mas_equalTo(size.height);
    }];
    [self.passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.left.equalTo(UserName.mas_right).offset(25);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passWord.mas_bottom);
        make.left.equalTo(0);
        make.width.mas_equalTo(MainW);
        make.height.mas_equalTo(1);
    }];
    [AcooutName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottom.mas_centerY).offset(25);
        make.left.equalTo(10);
        make.width.mas_equalTo(size.width+5);
        make.height.mas_equalTo(size.height);
    }];
    [self.passWord1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(50);
        make.left.equalTo(AcooutName.mas_right).offset(25);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    [MySureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bottom.mas_bottom).offset(40);
        make.right.mas_equalTo(-10);
        make.left.mas_equalTo(10);
        make.height.mas_equalTo(50);
    }];

}
-(void)SureClick{
    NSString *strUrl = [self.passWord.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    //NSLog(@"%@------%@",strUrl,self.passWord1.text);
    if (strUrl.length==0||self.passWord1.text.length==0) {
        [self showToast:MyString(@"请完善银行卡信息")];
        return;
    }
    if (self.label.text==nil) {
        [self showToast:MyString(@"请确定卡号是否正确")];
        return;
    }
    ZJSecuritycodeViewController*vc=[[ZJSecuritycodeViewController alloc]init];
    vc.dic=[NSMutableDictionary dictionary];
    [vc.dic setObject:self.passWord1.text forKey:@"creditName"];
    [vc.dic setObject:strUrl forKey:@"creditAccount"];
    [vc.dic setObject:self.label.text forKey:@"creditBank"];
    vc.curphoneStr=[ZJUserDefaultsTools getCurrentUserPhone];
    vc.binkCart=1;
    //vc.binkOrg=3;
    [self.navigationController pushViewController:vc animated:YES];
    //NSLog(@"下一步");
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //判断是否是当前输入框
    if (self.passWord == textField) {
        NSString *text = self.passWord.text;
        //限制字符 至于那个\b用在search中 写不写都行
        NSCharacterSet* characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        //去掉空格
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        //检查除数字之外的字符 invertedSet:意思是取反,除了数字和退格的内容
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            return NO;
        }
        //拼接text  要输入的部分+原有的
        text = [text stringByReplacingCharactersInRange:range withString:string];
        //去空格
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        self.bankString=text;
        [self hiddenBankLable:self.bankString];
        if (text.length==16||text.length==19) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self];
            [self performSelector:@selector(yanchi:) withObject:text afterDelay:0.3];
            
        }
        NSString* newString = @"";
        //四位添加空格
        while (text.length > 0) {
            NSString* subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        //去掉除数字部分
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        // 限制长度
        if (self.passWord.text.length >= 24) {
            //return NO;
        }
        //赋值
        [self.passWord setText:newString];
        
        return NO;
    }
    return YES;
}
-(void)yanchi:(NSString*)text{
    [self checkBankRequest:text];
}
-(void)checkBankRequest:(NSString *)text{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"creditAccount"]=text;
    [self doHttpRequestWithApi:CHECKBANKINFO method:GET dic:dic successBlock:^(NSDictionary *dic) {
        if (dic[@"data"][@"creditBank"]!=nil) {
            self.label.hidden=NO;
            self.bankImage.hidden=NO;
            self.label.text=dic[@"data"][@"creditBank"];
            [self.bankImage sd_setImageWithURL:[NSURL URLWithString:dic[@"data"][@"logo"]]];
        }else{
            self.label.hidden=YES;
            self.label.text=nil;
            self.bankImage.hidden=YES;;
            
        }
        //NSLog(@"变化了吗");
    }];
    
}
-(void)hiddenBankLable:(NSString *)string{
    if (string.length!=16||string.length!=19) {
        self.label.hidden=YES;
        self.label.text=nil;
        self.bankImage.hidden=YES;
    }
}
@end

